package cn.fyg.service.finance.domain.deposit.impl;

import java.io.StringReader;
import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.internal.Streams;
import com.google.gson.stream.JsonReader;

import cn.fyg.common.base.retv.Retv;
import cn.fyg.service.finance.config.Connect;
import cn.fyg.service.finance.domain.deposit.DepositDto;
import cn.fyg.service.finance.domain.deposit.DepositPjct;
import cn.fyg.service.finance.domain.deposit.DepositService;
import cn.fyg.service.finance.domain.deposit.DepositdetailDto;
import cn.fyg.service.finance.domain.salary.ProjectDto;
import cn.fyg.service.finance.domain.salary.SearchBean;
import cn.fyg.service.finance.util.DES;
import cn.fyg.service.finance.util.GsonUtil;
@Component
public class DepositServiceImpl implements DepositService{
	public static final String REST_SERVICE_URI = new Connect().getUrl();
	@Override
	public Retv<DepositDto> getDepositlist(String accountnumber) {
		// TODO Auto-generated method stub
		SearchBean bean = new SearchBean();
		bean.setAccountnumber(accountnumber);
		bean.setAmountType("");
		bean.setProjectnumber("");
		String result = this.doPost(bean);
		if (result.contains("detail")) {
			JsonElement je = Streams.parse(new JsonReader(new StringReader(result)));
			JsonObject jo = je.getAsJsonObject();
			List<ProjectDto> detail = GsonUtil.fromJsonList(jo.get("detail").toString(), ProjectDto.class);
//			for (ProjectDto pj : detail) {
//				if (pj.getInitAssistBalance().equals(null)) {
//					pj.setInitAssistBalance("0");
//				}
//				if (pj.getInvoiceAmount().equals(null)) {
//					pj.setInvoiceAmount("0");
//				}
//				if (pj.getReceivedAmount().equals(null)) {
//					pj.setReceivedAmount("0");
//				}
//				if (pj.getUnReceivedAmount().equals(null)) {
//					pj.setUnReceivedAmount("0");
//				}
//			}
			DepositDto deposit = new DepositDto();
			deposit.setDetail(detail);
			deposit.setTotalInitAssistBalance(jo.get("totalInitAssistBalance").toString());
			deposit.setTotalInvoiceAmount(jo.get("totalInvoiceAmount").toString());
			deposit.setTotalReceivedAmount(jo.get("totalReceivedAmount").toString());
			deposit.setTotalUnReceivedAmount(jo.get("totalUnReceivedAmount").toString());
			return Retv.data(deposit);
		}
		return Retv.error("当前账户下没有数据");
	}

	@Override
	public Retv<DepositPjct> getDepositdetail(String pjNum) {
		// TODO Auto-generated method stub
		SearchBean bean = new SearchBean();
		bean.setAmountType("");
		bean.setAccountnumber("");
		//bean.setAccountid("");
		bean.setProjectnumber(pjNum);
		String result = this.doPost(bean);
		if (result.contains("detail")) {
			JsonElement je = Streams.parse(new JsonReader(new StringReader(result)));
			JsonObject jo = je.getAsJsonObject();
			List<DepositdetailDto> detail = GsonUtil.fromJsonList(jo.get("detail").toString(), DepositdetailDto.class);
//			for (DepositdetailDto d : detail) {
//				if (d.getIsInvoiceAmount() == "false") {
//					d.setIsInvoiceAmount("开票: ");
//				}else {
//					d.setIsInvoiceAmount("已收: ");
//				}
//			}
			DepositPjct deposit = new DepositPjct();
			deposit.setDetail(detail);
			deposit.setInitAssistBalance(jo.get("initAssistBalance").toString());
			deposit.setTotalInvoiceAmount(jo.get("totalInvoiceAmount").toString());
			deposit.setTotalReceivedAmount(jo.get("totalReceivedAmount").toString());
			deposit.setTotalUnReceivedAmount(jo.get("totalUnReceivedAmount").toString());
			return Retv.data(deposit);
		}
		return Retv.error("当前项目下没有数据");
	}
	
	private String doPost(SearchBean bean) {
		RestTemplate restTemplate = new RestTemplate();
		
		String result = "";
		if (!bean.getAccountnumber().isEmpty() && bean.getAmountType().isEmpty() && bean.getProjectnumber().isEmpty()) {
			String result_encrypted = restTemplate.postForObject(REST_SERVICE_URI + "/getDepoistReceived?accountnumber={accountnumber}&isGetAllProject=true&fyg=fyg", null, String.class, bean.getAccountnumber());
			try {
				result = DES.decryptDES(result_encrypted, DES.key);
			} catch (Exception e) {
				System.out.println("error");
			}
		} else if (bean.getAccountnumber().isEmpty() && bean.getAmountType().isEmpty() && !bean.getProjectnumber().isEmpty()) {
			String result_encrypted = restTemplate.postForObject(REST_SERVICE_URI + "/getDepoistReceived?projectnumber={projectnumber}&isGetAllProject=false&fyg=fyg", null, String.class, bean.getProjectnumber());
			try {
				result = DES.decryptDES(result_encrypted, DES.key);
			} catch (Exception e) {
				System.out.println("error");
			}
		} else if (!bean.getAccountnumber().isEmpty() && !bean.getAmountType().isEmpty() && bean.getProjectnumber().isEmpty()) {
			String result_encrypted = restTemplate.postForObject(REST_SERVICE_URI + "/getDepoistReceived?accountnumber={accountnumber}&amountType={amountType}&begYear=2008&begMonth=12&begDate=20&endYear=2150&endMonth=12&endDate=31&isGetAllProject=byDate&fyg=fyg", null, String.class, bean.getAccountnumber(), bean.getAmountType());
			try {
				result = DES.decryptDES(result_encrypted, DES.key);
			} catch (Exception e) {
				System.out.println("error");
			}
		}
		
		
		return result;
	}

	@Override
	public String test() {
		// TODO Auto-generated method stub
		SearchBean bean = new SearchBean();
		bean.setAccountnumber("00853");
		bean.setAmountType("receivedAmount");
		bean.setProjectnumber("");
		String result = this.doPost(bean);
		return result;
	}

	@Override
	public Retv<DepositPjct> getDepositProject(String accountnumber, String amountType) {
		// TODO Auto-generated method stub
		SearchBean bean = new SearchBean();
		bean.setAccountnumber(accountnumber);
		bean.setAmountType(amountType);
		bean.setProjectnumber("");
		String result = this.doPost(bean);
		if (result.contains("detail")) {
			JsonElement je = Streams.parse(new JsonReader(new StringReader(result)));
			JsonObject jo = je.getAsJsonObject();
			List<DepositdetailDto> detail = GsonUtil.fromJsonList(jo.get("detail").toString(), DepositdetailDto.class);
			DepositPjct deposit = new DepositPjct();
			deposit.setDetail(detail);
			
			deposit.setTotalInvoiceAmount(jo.get("totalInvoiceAmount").toString());
			deposit.setTotalReceivedAmount(jo.get("totalReceivedAmount").toString());
			return Retv.data(deposit);
		}
		return Retv.error("当前账户下没有数据");
	}
}
